home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / networking / pgpuam / sources / passphrasecache.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  7.4 KB  |  293 lines

  1. //    PassphraseCache.c -  Passphrase Cache Interface Object  
  2. // 
  3. // Apple Macintosh Developer Technical Support
  4. // Written by:  Vinnie Moscaritolo
  5. //
  6. //  Copyright (work in progress)  Apple Computer, Inc All rights reserved.
  7. //
  8. // You may incorporate this sample code into your applications without
  9. // restriction, though the sample code has been provided "AS IS" and the
  10. // responsibility for its operation is 100% yours.  However, what you are
  11. // not permitted to do is to redistribute the source as "DSC Sample Code"
  12. // after having made changes. If you're going to re-distribute the source,
  13. // we require that you make it clear in the source that the code was
  14. // descended from Apple Sample Code, but that you've made changes.
  15. // 
  16.  
  17. #include <string.h>
  18. #include <Timer.h>
  19. #include <CodeFragments.h>
  20. #include <Errors.h>
  21.  
  22. #define PGP_MACINTOSH 1
  23. #include "pgpkeys.h"
  24. #include "pgpUtilities.h"
  25. #include "pgpMemoryMgr.h"
  26.  
  27. // dont include the .h else the linker gets confused..
  28. //#include "PassphraseCache.h"
  29.  
  30. // ---------------------------------------------------------------------------
  31. #pragma mark Exported Symbols
  32. // ---------------------------------------------------------------------------
  33.  
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37.  
  38. #pragma export on
  39.  
  40.     OSErr         __passphrasecache_initialize(const CFragInitBlock* initBlock);
  41.     void         __passphrasecache_terminate(void);
  42.  
  43.     void      FlushPassphraseCache();
  44.  
  45.     void     EnablePassphraseCaching( Boolean  enable);
  46.     void      SetPassphraseCacheTimeLimit( SInt16 mins);
  47.  
  48.     void      RememberPassphrase (PGPKeyRef  keyRef, const char* passphrase);        
  49.  
  50.     Boolean  GetPassphrase      (PGPContextRef, PGPKeyRef, char** passphrase);        
  51.     
  52.     pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
  53.     pascal OSErr __terminate(void);
  54.   
  55.  
  56. #pragma export off
  57.  
  58. #ifdef __cplusplus
  59. }
  60. #endif
  61.  
  62.  
  63. #define k1Minute        (1000  * 60) /* actually 60 */ 
  64.  
  65. // ---------------------------------------------------------------------------
  66. #pragma mark Globals
  67. // ---------------------------------------------------------------------------
  68.     TMTask                gTMTask = {nil,0,nil,0,0};
  69.     long                gCacheTimeLimit;
  70.     UInt32                gWatchdogTick;
  71.     UInt32                gExpired;
  72.     
  73.     
  74.      char                gCache[256];
  75.  
  76.  
  77.     CFragConnectionID    gConnID         = nil;
  78.     Boolean                gCachingEnabled = false;
  79.     FSSpec                gFragSpec;
  80.     UInt32                 gFragOffset;
  81.     UInt32                gFragLength;
  82.  
  83. // ---------------------------------------------------------------------------
  84. #pragma mark Local Prototypes
  85. // ---------------------------------------------------------------------------
  86.  
  87. static void __passphrasecache_timerProc(void);
  88. static void     ResetWatchDog(void);
  89.  
  90.  
  91. // ---------------------------------------------------------------------------
  92. static void __passphrasecache_timerProc(void)
  93. // ---------------------------------------------------------------------------
  94. //
  95. {
  96.     FlushPassphraseCache();
  97. //    DebugStr("\pTimer Expires");
  98. }
  99.  
  100.  
  101. // ---------------------------------------------------------------------------
  102.  void FlushPassphraseCache() 
  103. // ---------------------------------------------------------------------------
  104. //  shutdown  Passphrase Cache Interface Object  
  105. {
  106.      gCache[0] = '\0';
  107.  
  108. }
  109.  
  110.  
  111.  // ---------------------------------------------------------------------------
  112. OSErr __passphrasecache_initialize(const CFragInitBlock* initBlock)
  113. // ---------------------------------------------------------------------------
  114. //
  115. //
  116.  
  117. {
  118. #pragma unused (initBlock)
  119.     OSStatus                ErrNo = noErr;
  120.     
  121.     ErrNo = __initialize( initBlock );
  122.      if( ErrNo != noErr) return ErrNo;
  123.  
  124. //DebugStr("\p__passphrasecache_initialize");
  125.  
  126.     if( initBlock->fragLocator.where != kDataForkCFragLocator)    
  127.         {
  128.             DebugStr("\pError Could't load PassphraseCacheLib, wrong frag type?\n");
  129.             return cfragFragmentUsageErr;
  130.         }
  131.  
  132.     gFragSpec = *initBlock->fragLocator.u.onDisk.fileSpec;
  133.      gFragOffset    = initBlock->fragLocator.u.onDisk.offset,
  134.     gFragLength = initBlock->fragLocator.u.onDisk.length,
  135.     
  136.     gCache[0] = '\0';
  137.   
  138.      return ErrNo;
  139. }
  140.  
  141. // ---------------------------------------------------------------------------
  142. void __passphrasecache_terminate(void)
  143. // ---------------------------------------------------------------------------
  144. //
  145. //
  146.  
  147. {
  148. //    DebugStr("\p__passphrasecache_terminate");
  149.     
  150.     if(gTMTask.tmAddr != nil)
  151.     {
  152.         RmvTime((QElemPtr) &gTMTask);
  153.          DisposeRoutineDescriptor(gTMTask.tmAddr);
  154.          gTMTask.tmAddr = nil;
  155.      }
  156.      
  157.      __terminate();
  158.     
  159. }
  160.  
  161. #pragma mark -
  162.  
  163.  
  164. // ---------------------------------------------------------------------------
  165. static void ResetWatchDog(void)
  166. // ---------------------------------------------------------------------------
  167. //   
  168. {
  169.     if(gCachingEnabled)
  170.     {
  171.         RmvTime((QElemPtr) &gTMTask);
  172.            InsTime((QElemPtr) &gTMTask);            
  173.         PrimeTime((QElemPtr)&gTMTask, gCacheTimeLimit);      
  174.      }
  175. }
  176.  
  177. // ---------------------------------------------------------------------------
  178. void EnablePassphraseCaching( Boolean  enable) 
  179. // ---------------------------------------------------------------------------
  180. //   
  181. {
  182.     THz    saveZone;
  183.     OSStatus    ErrNo = noErr;
  184.      
  185. // this needs to be done in system heap
  186.     saveZone = GetZone();
  187.     SetZone( SystemZone() );
  188.  
  189.     if(gCachingEnabled &&  !enable)    
  190.     {
  191.         // turn caching off
  192.         RmvTime((QElemPtr) &gTMTask);
  193.          DisposeRoutineDescriptor(gTMTask.tmAddr);
  194.          gTMTask.tmAddr = nil;
  195.          gCachingEnabled = false;
  196.         FlushPassphraseCache();
  197.  
  198.         CloseConnection(&gConnID);
  199.      }
  200.     else if(enable && !gCachingEnabled)
  201.     {
  202.         ProcPtr codePtr;
  203.         Str255    errorMessage;
  204.         
  205. // create persistant connection by   increment library  refcount
  206.          ErrNo = GetDiskFragment( &gFragSpec,gFragOffset, gFragLength,
  207.                                 "\pPassphraseCacheLib", 
  208.                                 kReferenceCFrag, 
  209.                                 &gConnID, 
  210.                                 &(Ptr)codePtr, 
  211.                                 errorMessage);
  212.     if (ErrNo != noErr)
  213.         {
  214.             DebugStr("\pError  Could't load PassphraseCacheLib\n");
  215.         }
  216.  
  217. // start the caching timer up.
  218.         gTMTask.tmAddr         =  NewTimerProc( __passphrasecache_timerProc );  
  219.         gCachingEnabled = true;
  220.      }
  221.     
  222.      SetZone(saveZone);
  223. }
  224.  
  225. // ---------------------------------------------------------------------------
  226.  void SetPassphraseCacheTimeLimit( SInt16 mins) //  (static public)
  227. // ---------------------------------------------------------------------------
  228. //   
  229. {
  230.  
  231.      gCacheTimeLimit = mins * 1000 /* * 60 */ ;
  232.  
  233.      if(mins == 0)  // don't cache
  234.      {
  235.            
  236.       // take this out to cache passphrase
  237.     };
  238.      
  239.      if(mins == 90)  // no time limit
  240.      {
  241.      };  
  242.      
  243. }
  244.  
  245.  
  246.  
  247. // ---------------------------------------------------------------------------
  248.  void RememberPassphrase(PGPKeyRef  keyRef, const char* passphrase)   //  (static public)
  249. // ---------------------------------------------------------------------------
  250. //   
  251. {
  252.      PGPPublicKeyAlgorithm algorithm;
  253.     PGPKeyID              keyID;
  254.      
  255.     PGPGetKeyNumber (keyRef,  kPGPKeyPropAlgID,  (PGPInt32*) &algorithm);
  256.     PGPGetKeyIDFromKey (keyRef, &keyID );
  257.      
  258.     strcpy (gCache, passphrase);
  259.      
  260.       ResetWatchDog();
  261. }
  262.  
  263. // ---------------------------------------------------------------------------
  264.  Boolean GetPassphrase (PGPContextRef    context, PGPKeyRef  keyRef, char** passphrase)   //  (static public)
  265. // ---------------------------------------------------------------------------
  266. //   
  267. {
  268.      PGPPublicKeyAlgorithm algorithm;
  269.     PGPKeyID              keyID;
  270.      
  271.     ResetWatchDog();
  272.  
  273.     PGPGetKeyNumber (keyRef,  kPGPKeyPropAlgID,  (PGPInt32*) &algorithm);
  274.     PGPGetKeyIDFromKey (keyRef, &keyID );
  275.  
  276.     if( strlen(gCache) > 0)
  277.     {
  278.  
  279.         *passphrase     = (char*) PGPNewSecureData( PGPGetContextMemoryMgr(context),  strlen(gCache) , kPGPMemoryMgrFlags_None );
  280.          strcpy (*passphrase, gCache);
  281.          return true;
  282.     }
  283. //      if(  PGPCompareKeyIDs(keyID, keyID) == 0);
  284.  
  285.  
  286.  
  287. return false;
  288. }
  289.  
  290.  
  291.  
  292.  
  293.